/**
 * 渠道运营－申诉处理-未处理
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    require("pagination");
    var util = require('util');
    var ui = require('ui');
    var dialog = require("dialog");
    var angular = require("angular");
    require("permission");
    var api_prefix = G_CONFIG.api_prefix + "qmyx/broker/";

    angular.module('Treated', ['ngUI'])
        .controller('TreatedController', ['$rootScope', '$scope', '$http', 'httpPost','$location', 'projectList',
            function ($rootScope, $scope, $http, httpPost,$location, projectList) {
                $scope.search = {
                    search_field: "cst_name",
                    keyword: "",
                    fieldList: []
                };
                //经济人身份
                $scope.changeBrokerIdentity = function (event, value, record) {
                    $scope.filter.capacityDes = [];
                    //获取经济人下级菜单
                    $scope.selectedBrokerIdentity = value;
                    $scope.filter.capacityDes = [value];
                    $scope.getOwerCompanys = function () {
                        for (var i = 0, item; item = $scope.brokerIdentity[i]; i++) {
                            switch ($scope.selectedBrokerIdentity) {
                                case '9003':
                                    // 代理公司
                                    return record.items || [];

                                case '9005':
                                    // 中介公司
                                    return record.items || [];

                                case '9006':
                                    // 合作伙伴
                                    return record.items || [];

                                case '9108':
                                    // 中介机构
                                    return record.items || [];
                                default:
                                    return [];
                            }
                        }
                    };

                    $scope.owerCompanys = $scope.getOwerCompanys();

                    for (var i = 0; i < $scope.owerCompanys.length; i++) {
                        $scope.owerCompanys[i].checked = false;
                    }
                    $scope.owerCompanyCheckAll = false;
                    if (event) {
                        ui.changeTipsArrow(event.currentTarget);
                    }
                };
                // 全选/取消全选
                $scope.updateOwerCompanyCheckAll = function () {
                    var filterBelongsCompany = [];
                    for (var i = 0, item; item = $scope.owerCompanys[i]; i++) {
                        item.checked = $scope.owerCompanyCheckAll;
                        if (item.checked) {
                            filterBelongsCompany.push(item.full_code)
                        }
                    }
                    if (filterBelongsCompany.length == 0) {
                        filterBelongsCompany = [$scope.selectedBrokerIdentity];
                    }
                    $scope.filter.capacityDes = filterBelongsCompany;
                };

                // 同步全选/取消全选
                $scope.updateOwerCompanyCheck = function () {
                    var checked = true;
                    var filterBelongsCompany = [];
                    for (var i = 0, item; item = $scope.owerCompanys[i]; i++) {
                        if (!item.checked) {
                            checked = false;
                        } else {
                            filterBelongsCompany.push(item.full_code);
                        }
                    }

                    if (filterBelongsCompany.length == 0) {
                        filterBelongsCompany = [$scope.selectedBrokerIdentity];
                    }
                    $scope.filter.capacityDes = filterBelongsCompany;
                    $scope.owerCompanyCheckAll = checked;

                };


                //搜索
                function searchParams() {
                    return {
                        project_id: $scope.projectId,
                        pageIndex: $scope.pageIndex - 1,
                        pageSize: $scope.filter.pageSize,
                        search_text: $scope.search.keyword,
                        search_field: $scope.search.search_field,
                        capacity_des_arr: [],
                        begin_appeal_date: '',
                        end_appeal_date: '',
                        isdeal: true,
                        appeal_result: []
                    };
                }

                function doGetSearchData() {
                    doGetData(searchParams());
                }

                /*搜索按钮点击*/
                $scope.getSearchData = function () {
                    $scope.pageIndex = 1;
                    $scope.searchStatus = '1';
                    doGetSearchData();
                };

                //客户状态
                $scope.resultList = [
                    {Text: '申诉成功', selected: false, resultStatus: "1"},
                    {Text: '拒绝申诉', selected: false, resultStatus: "0"},
                ];
                $scope.updataResultCheck = function () {
                    var resultList = $scope.resultList;
                    var filterResultStatus = [];
                    for (var i = 0, item; item = resultList[i]; i++) {
                        if (item.selected) {
                            filterResultStatus.push(item.resultStatus);
                        }
                    }
                    if (filterResultStatus.length == 0) {
                        filterResultStatus = [];
                    }
                    $scope.filter.appeal_result = filterResultStatus;
                };


                //查询默认
                $scope.filter = {
                    capacityDes: [],
                    appeal_result: [],
                    startDate: '',
                    endDate: '',
                    pageSize: 10,
                    // 撤销过滤
                    clearFilter: function () {
                        for (var i = 0, item; item = $scope.resultList[i]; i++) {
                            item.selected = false
                        }
                        $scope.selectedBrokerIdentity = '';
                        $scope.owerCompanys = [];
                        $scope.filter.capacityDes = [];
                        $scope.filter.appeal_result = [];
                        $scope.filter.startDate = '';
                        $scope.filter.endDate = '';
                        $scope.getSearchData();
                    }
                };

                //查询
                function filterParams() {
                    return {
                        project_id: $scope.projectId,
                        pageIndex: $scope.pageIndex - 1,
                        pageSize: $scope.filter.pageSize,
                        search_text: '',
                        search_field: $scope.search.search_field,
                        capacity_des_arr: $scope.filter.capacityDes,
                        begin_appeal_date: $scope.filter.startDate,
                        end_appeal_date: $scope.filter.endDate,
                        isdeal: true,
                        appeal_result: $scope.filter.appeal_result
                    };
                }

                function doGetFilterData() {
                    doGetData(filterParams());
                }

                //点击查询按钮
                $scope.getFilterData = function () {
                    $scope.pageIndex = 1;
                    $scope.searchStatus = '2';
                    doGetFilterData();
                };

                /*分页*/
                $scope.getBrokerData = function (pageIndex) {
                    $scope.pageIndex = pageIndex;
                    switch ($scope.searchStatus) {
                        case '0':
                            getData();
                            break;
                        case '1':
                            doGetSearchData();
                            break;
                        case '2':
                            doGetFilterData();
                            break;
                    }
                };

                //导出经纪人列表
                $scope.exportData = function (e) {
                    switch ($scope.searchStatus) {
                        case '0':
                            var params = defaultParams();
                            util.download(api_prefix + "export-appeal", {filter: JSON.stringify(params)});
                            break;
                        case '1':
                            var params = searchParams();
                            util.download(api_prefix + "export-appeal", {filter: JSON.stringify(params)});
                            break;
                        case '2':
                            var params = filterParams();
                            util.download(api_prefix + "export-appeal", {filter: JSON.stringify(params)});
                            break;
                    }

                };

                /*获取申诉列表过滤条件设置*/
                $scope.loading = true;
                $http
                    .get(api_prefix + "get-appeal-filter-setting")
                    .then(function (res) {
                        $scope.loading = false;
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            var data = res.data.data;
                            $scope.searchFields = data.searchFields;
                            $scope.brokerIdentity = data.capacities;

                            //搜索下拉选项数据
                            var searchFields = $scope.searchFields;
                            $scope.search.fieldList = [
                                {value: 'cst_name', text: searchFields.cst_name},
                                {value: 'mobile_tel', text: searchFields.mobile_tel},
                                {value: 'bro_broker_name', text: searchFields.bro_broker_name},
                                {value: 'bro_mobile_tel', text: searchFields.bro_mobile_tel}
                            ]
                        } else {
                            ui.showErrTip(res.data.errMsg);
                            return false;
                        }
                    })
                    .finally(function () {
                        $scope.loading = false;
                    });


                function doGetData(data) {
                    var pageIndex = data.pageIndex,
                        pageSize = data.pageSize;
                    $scope.loading = true;
                    httpPost(
                            api_prefix + "query-appeal-page-list&pageIndex=" + pageIndex + "&pageSize=" + pageSize, data
                    )
                        .success(function (res) {
                            $scope.loading = false;
                            var retCode = res.retCode;
                            if (retCode == 0) {
                                var data = res.data;
                                $scope.list = data.items;
                                $scope.total = data.total;
                            } else {
                                ui.showErrTip(res.errMsg);
                            }
                        })
                }


                /**
                 * 申诉处理
                 * @param data
                 * @param dialog
                 */
                $scope.dealComplaint = function (item, dialog, $dialogScope) {

                    /*dialog.okValue = '接受申诉';
                     dialog.cancelValue = '拒绝申诉';*/

                    var data = {
                        id: item.appeal_id,
                        resultDesc: item.result_detail
                    };
                    httpPost(
                            api_prefix + "accept-appeal&id=" + data.id + "&resultDesc=" + data.resultDesc
                    )
                        .success(function (res) {
                            var retCode = res.retCode;
                            if (retCode == 0) {
                                ui.showErrTip();
                            } else {
                                ui.showErrTip(res.errMsg);
                            }
                        })
                };


                //列表默认参数
                function defaultParams() {
                    return {
                        project_id: $scope.projectId,
                        pageIndex: $scope.pageIndex - 1,
                        pageSize: $scope.filter.pageSize,
                        search_text: '',
                        search_field: $scope.search.search_field,
                        capacity_des_arr: [],
                        begin_appeal_date: '',
                        end_appeal_date: '',
                        isdeal: true,
                        appeal_result: []
                    };
                }

                function getData() {
                   doGetData(defaultParams());
                }

                //获取列表数据
                var update = function (projectId) {
                    $scope.projectId = projectId;
                    $scope.pageIndex = 1;
                    $scope.searchStatus = '0';
                    getData();
                };

                $scope.$watch('curProjectId', function () {
                    if ($scope.curProjectId) {
                        update($scope.curProjectId);
                    }
                });

            }
        ])
        .factory('httpPost', ['$http', function ($http) {
            return function (url, data) {
                if (!data) {
                    data = {};
                }
                data = $.extend(data, {"USE_FORM_DATA": false});
                return $http.post(url, data);
            }
        }]);
})
